package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.AdminPreidCourse;
import com.ruoyi.system.domain.AdminQuestion;
import com.ruoyi.system.service.IAdminPreidCourseService;
import com.ruoyi.system.service.IAdminQuestionService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 选择题Controller
 * 
 * @author ruoyi
 * @date 2020-06-17
 */
@Controller
@RequestMapping("/system/question")
public class AdminQuestionController extends BaseController
{
    private String prefix = "system/question";

    @Autowired
    private IAdminQuestionService adminQuestionService;
    @Autowired
    private IAdminPreidCourseService adminPreidCourseService;

    @RequiresPermissions("system:question:view")
    @GetMapping()
    public String question()
    {
        return prefix + "/question";
    }

    /**
     * 查询选择题列表
     */
    @RequiresPermissions("system:question:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdminQuestion adminQuestion)
    {
        startPage();
        List<AdminQuestion> list = adminQuestionService.selectAdminQuestionList(adminQuestion);
        return getDataTable(list);
    }

    /**
     * 导出选择题列表
     */
    @RequiresPermissions("system:question:export")
    @Log(title = "选择题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdminQuestion adminQuestion)
    {
        List<AdminQuestion> list = adminQuestionService.selectAdminQuestionList(adminQuestion);
        ExcelUtil<AdminQuestion> util = new ExcelUtil<AdminQuestion>(AdminQuestion.class);
        return util.exportExcel(list, "question");
    }

    /**
     * 查看详细
     */
    @GetMapping("/detail/{questionId}")
    public String detail(@PathVariable("questionId") Long questionId, ModelMap mmap)
    {
        mmap.put("question", adminQuestionService.selectAdminQuestionById(questionId));
        return prefix + "/detail";
    }
    
    /**
     * 新增选择题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存选择题
     */
    @RequiresPermissions("system:question:add")
    @Log(title = "选择题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdminQuestion adminQuestion)
    {
    	adminQuestion.setCompanyId(ShiroUtils.getCompanyId());
    	adminQuestion.setCompanyName(ShiroUtils.getCompanyName());
    	adminQuestion.setUserId(ShiroUtils.getUserId());
    	adminQuestion.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(adminQuestionService.insertAdminQuestion(adminQuestion));
    }

    /**
     * 修改选择题
     */
    @GetMapping("/edit/{questionId}")
    public String edit(@PathVariable("questionId") Long questionId, ModelMap mmap)
    {
        AdminQuestion adminQuestion = adminQuestionService.selectAdminQuestionById(questionId);
        mmap.put("adminQuestion", adminQuestion);
        return prefix + "/edit";
    }

    /**
     * 修改保存选择题
     */
    @RequiresPermissions("system:question:edit")
    @Log(title = "选择题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdminQuestion adminQuestion)
    {
    	adminQuestion.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(adminQuestionService.updateAdminQuestion(adminQuestion));
    }

    /**
     * 删除选择题
     */
    @RequiresPermissions("system:question:remove")
    @Log(title = "选择题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adminQuestionService.deleteAdminQuestionByIds(ids));
    }
    
    /**
     * 选择题状态修改
     */
    @Log(title = "选择题管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:question:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(AdminQuestion adminQuestion)

    {
        return toAjax(adminQuestionService.changeStatus(adminQuestion));
    }

    /**
     * 我的课程- 添加选择题-选择题列表
     * @param preidId
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:music:view")
    @GetMapping("/{preidId}")
    public String question(@PathVariable("preidId") Long preidId, ModelMap mmap)
    {
      mmap.put("preidId", preidId);
      return prefix + "/question2";
    }

    /**
     * 新增保存曲目
     */
    @RequiresPermissions("system:video:add")
    @Log(title = "曲目", businessType = BusinessType.INSERT)
    @PostMapping("/add2")
    @ResponseBody
    public AjaxResult addSave2(AdminQuestion adminQuestion)
    {

      AdminPreidCourse adminPreidCourse = new AdminPreidCourse ();
      adminPreidCourse.setCourseId(adminQuestion.getQuestionId());
      adminPreidCourse.setCourseName(adminQuestion.getQuestionName());
      adminPreidCourse.setCourseType("3");
      adminPreidCourse.setPreidId(adminQuestion.getPreidId());
      adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());


      return toAjax(adminPreidCourseService.insertAdminPreidCourse(adminPreidCourse));
    }



    /**
     * 查询曲目列表
     */
    @RequiresPermissions("system:video:list")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(AdminQuestion adminQuestion)
    {
      startPage();
      List<AdminQuestion> list = adminQuestionService.selectAdminQuestionList(adminQuestion);
      AdminPreidCourse adminPreidCourse = new AdminPreidCourse();
      for (AdminQuestion question : list) {
        adminPreidCourse.setCourseId(question.getQuestionId());
        adminPreidCourse.setPreidId(adminQuestion.getPreidId());
        adminPreidCourse.setCourseType("3");
        adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());
        List<AdminPreidCourse> adminPreidCourses = adminPreidCourseService.selectAdminPreidCourseList(adminPreidCourse);
        if(null!=adminPreidCourse && adminPreidCourses.size()>0){
          question.setAddPeriedFlag("1");
        }

      }
      return getDataTable(list);
    }

    /**
     * 删除课时曲目
     */
    @RequiresPermissions("system:video:remove")
    @Log(title = "曲目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove2")
    @ResponseBody
    public AjaxResult remove2(AdminQuestion adminQuestion)
    {
      AdminPreidCourse adminPreidCourse = new AdminPreidCourse ();
      adminPreidCourse.setCourseId(adminQuestion.getQuestionId());
      adminPreidCourse.setPreidId(adminQuestion.getPreidId());
      adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());


      return toAjax(adminPreidCourseService.deleteAdminPreidCourse(adminPreidCourse));
    }
}
